package com.oa.hyperion.api.v1;

import com.oa.hyperion.entity.assets.AssetsGeneralLedger;
import com.oa.hyperion.entity.assets.AssetsScrap;
import com.oa.hyperion.entity.assets.AssetsType;
import com.oa.hyperion.service.AssetsGeneralLedgerService;
import com.oa.hyperion.service.AssetsScrapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 资产报废controller
 * author yuanchongwei
 * date 2022-1-19
 */
@Api(value="资产报废controller",tags={"资产报废操作接口"})
@RestController
@RequestMapping("/v1/assets/scrap")
public class AssetsScrapController {
    @Autowired
    private AssetsGeneralLedgerService assetsService;
    @Autowired
    private AssetsScrapService assetsScrapService;

    @ApiOperation("资产报废")
    @PostMapping("add")
    public String addAssetsScrap(@RequestBody AssetsScrap assetsScrap){
        if (assetsScrap == null) {
            return "参数为空";
        }
        //TODO 先判断资产类型，非办公用品类的继续判断原来的状态，最后判断资资产是否已在报废清单中
        AssetsGeneralLedger assetsGeneralLedger = assetsService.getAssetsById(assetsScrap.getAssetId());
        int type = assetsGeneralLedger.getType();
        if (type == 1) {
            //办公用品，报废的同时，总账表不修改状态，只修改库存
            assetsScrapService.insert(assetsScrap);
            assetsGeneralLedger.setStock(assetsGeneralLedger.getStock()-1);
            if(assetsService.update(assetsGeneralLedger)){
                return "报废成功，库存已更新";
            }
        }
        // 原有状态
        int state=assetsService.getAssetsById(assetsScrap.getAssetId()).getStatus();
        // 查询报废清单
        AssetsScrap as = assetsScrapService.getAssetsScrapByAssetId(assetsScrap.getAssetId());
        //确保资产当前状态为未报废，且在报废表中不存在
        if(state!=4 && as ==null){
            //1.将修改资产状态为报废

            assetsGeneralLedger.setId(assetsScrap.getAssetId());
            assetsGeneralLedger.setStatus(4);
            if (assetsService.update(assetsGeneralLedger)) {
                //2.将资产写入报废表中
                assetsScrapService.insert(assetsScrap);
                return "操作成功";
            }
            else{
                return "操作失败";
            }
        }
        else{
            return "资产不能申请报废，请核对信息";
        }

    }

    @ApiOperation("报废清单查询")
    @GetMapping("all")
    public List<AssetsScrap> getAllScrapInfo(){
        return assetsScrapService.getAssetsScrapInfo();
    }

    @ApiOperation("根据资产id查询报废信息")
    @GetMapping("assetid/{assetId}")
    public AssetsScrap getScrapByAssetId(@PathVariable int assetId){
        if (assetId == 0) {
            return null;
        }
        return assetsScrapService.getAssetsScrapByAssetId(assetId);
    }

    @ApiOperation("根据经办人查询报废信息")
    @GetMapping("handler/{handler}")
    public List<AssetsScrap> getScrapBy(@PathVariable String handler){
        if (handler.equals("") || handler == null) {
            return null;
        }
        return assetsScrapService.getAssetsScrapByHandler(handler);
    }

}
